//
//  ZMKNormalButton.swift
//  UIKitExtentions
//
//  Created by gupengling on 2021/7/29.
//

import UIKit

public class ZMKNormalButton: UIButton {
    private var oldFrame: CGRect?
    var backgroundLayer: CAGradientLayer?

    /// 非可编辑状态， Enabled = false
    @objc public var disableBackgroundColors: [UIColor] = [
        UIColor(red: 227/255.0, green: 218/255.0, blue: 244/255.0, alpha: 1.0),
        UIColor(red: 202/255.0, green: 193/255.0, blue: 223/255.0, alpha: 1.0)
    ]

    @objc public var normalBackgroundColors: [UIColor] = [
        UIColor.white
    ]

    @objc public var selectBackgroundColors: [UIColor] = [
        UIColor.white
    ]

    public override var isSelected: Bool {
        didSet {
            resetDefaultUI()
        }
    }

    public override var isEnabled: Bool {
        didSet {
            resetDefaultUI()
        }
    }

    public override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundLayer = CAGradientLayer()
        backgroundLayer?.startPoint = CGPoint(x: -0.05, y: 0.5)
        backgroundLayer?.endPoint = CGPoint(x: 1.05, y: 0.5)
//        backgroundLayer?.locations = [0, 1.0]
//        backgroundLayer?.colors = [
////            UIColor(red: 255/255.0, green: 131/255.0, blue: 131/255.0, alpha: 1.0).cgColor,
////            UIColor(red: 255/255.0, green: 49/255.0, blue: 96/255.0, alpha: 1.0).cgColor
//            UIColor(red: 254/255.0, green: 202/255.0, blue: 30/255.0, alpha: 1.0).cgColor,
//            UIColor(red: 255/255.0, green: 121/255.0, blue: 3/255.0, alpha: 1.0).cgColor
//        ]
        backgroundLayer?.frame = bounds
        layer.insertSublayer(backgroundLayer!, at: 0)
    }

    convenience init() {
        self.init(frame:.zero)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    public override func layoutSubviews() {
        super.layoutSubviews()
        if oldFrame == bounds {
            return
        }
        oldFrame = bounds
        backgroundLayer?.frame = bounds
        resetDefaultUI()
    }

    private var backgroundColors: [CGColor]? {
        didSet {
            if let backgroundColors = backgroundColors,
               let backgroundLayer = backgroundLayer {
                objc_sync_enter(self)
                let spce = 1.0 / Float(backgroundColors.count - 1)
                var loc: Float = 0.0
                var nums = [NSNumber]()
                for (index, _) in backgroundColors.enumerated() {
                    if index == backgroundColors.count - 1 {
                        nums.append(NSNumber(1.0))
                    } else {
                        nums.append(NSNumber(value: loc))
                    }
                    loc += spce
                }
                backgroundLayer.locations = nums
                backgroundLayer.colors = backgroundColors
                objc_sync_exit(self)
            }
        }
    }

    @objc public func setBackgroundColors(_ colors: [UIColor]) {
        objc_sync_enter(self)
        backgroundColors = colors.map { $0.cgColor }
        objc_sync_exit(self)
    }

    @objc public func resetDefaultUI() {
        if !isEnabled {
            setBackgroundColors(disableBackgroundColors)
            return
        }

        if isSelected {
            setBackgroundColors(selectBackgroundColors)
            return
        }

        setBackgroundColors(normalBackgroundColors)
    }
}
